#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
struct Edge{
  LL u,v,w;
}edge[maxn];
bool cmp(Edge A,Edge B){
    return A.w<B.w;
}
LL dp[maxn],bac[maxn],dd[maxn];
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n,m;cin>>n>>m;
   for(LL i=1;i<=m;i++){
      LL u,v,w;cin>>u>>v>>w;
      edge[i]={u,v,w};
   }
   sort(edge+1,edge+1+m,cmp);
   for(LL i=1;i<=m;i++){
       LL j=i;
       LL tot=0;
       while(j<=m&&edge[j].w==edge[i].w){
          bac[++tot]=j;
          j++;
       }
       for(LL j=1;j<=tot;j++){
          LL a=edge[bac[j]].u;LL b=edge[bac[j]].v;
          dd[b]=max(dd[b],dp[a]+1);
       }
       for(LL j=1;j<=tot;j++){
          LL v=edge[bac[j]].v;
          dp[v]=dd[v];
       }
       i=j-1;
   }
   LL ans=0;
   for(LL i=1;i<=n;i++){
      ans=max(ans,dp[i]);
   }
   cout<<ans<<"\n";
   return 0;
}
